package com.gehj.androidlibs;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;

import com.gehj.androidlibs.transform.RotateTransformer;
import com.gehj.androidlibs.transform.ScaleTransformer;

import org.jetbrains.annotations.NotNull;

public class BannerActivity extends AppCompatActivity {
    private ViewPager mVpMain;
    private int[] mResIds = new int[]{
            0xffff0000,
            0xff00ff00,
            0xff0000ff,
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_banner);
        mVpMain = findViewById(R.id.banner_viewpager);
        mVpMain.setOffscreenPageLimit(3); //解决闪一下的问题
        mVpMain.setPageMargin(40); //pager之间的间隔
        mVpMain.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return mResIds.length;
            }

            @Override
            public boolean isViewFromObject(@NonNull @NotNull View view, @NonNull @NotNull Object object) {
                return view==object;
            }

            @NonNull
            @NotNull
            @Override
            public Object instantiateItem(@NonNull @NotNull ViewGroup container,
                                          int position) {
                //完成View的创建
                View view = new View(container.getContext());
                view.setBackgroundColor(mResIds[position]);
                ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams
                        (ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.MATCH_PARENT);
                container.addView(view);
                return view;
            }

            @Override
            public void destroyItem(@NonNull ViewGroup container,
                                    int position, @NonNull Object object) {
                container.removeView((View) object);
            }

        });

        mVpMain.setPageTransformer(true,new RotateTransformer());

    }
}